# 定义函数来将 dcm 文件转换为 nii.gz 文件

# 遍历文件夹 A 下的所有三级子文件夹
$folderPath = "F:\MedAi\pancreas_all"
$dstFolderPath = "F:\MedAi\pancreas_ok\images"

$level1Folders = Get-ChildItem -Path $folderPath -Directory

# 病人
foreach ($level1Folder in $level1Folders) {
    $level2Folders = Get-ChildItem -Path $level1Folder.FullName -Directory

    #  MR
    foreach ($level2Folder in $level2Folders) {
        $level3Folders = Get-ChildItem -Path $level2Folder.FullName -Directory

        # DWI
        foreach ($level3Folder in $level3Folders) {
            # 情况1 ：
            # 输出文件夹: out/ZS21341234/  输出文件名: DWI/T1/T2
            $outputFilePath = Join-Path $dstFolderPath $level1Folder.Name


            # Write-Host "输入路径："$level3Folder.FullName
            # Write-Host "输出路径：$outputFilePath"
            # Write-Host "输出文件：" $level3Folder.Name

            # 创建输出文件夹
            if (-not (Test-Path $outputFilePath -PathType Container)) {
                New-Item -Path $outputFilePath -ItemType Directory
            }

            # dcm2niix -o [输出文件夹] -z y -f [文件名]  [输入文件夹]
            # -b n: no json
            dcm2niix -b n -ba n -o $outputFilePath -z y -f $level3Folder.Name -s y $level3Folder.FullName

            # dcm2niix -b n -ba n -o 'D:\ace\Desktop\patch_mask1' -z y -f 'ZS10295523+DWI' -s y 'F:\MedAi\pancreas_all\ZS10295523\MR\DWI'
        }
    }
}
